/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.company.workflow.loader;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.workflow.InvalidWorkflowDescriptorException;
import com.opensymphony.workflow.loader.ValidationHelper;
import com.opensymphony.workflow.util.Validatable;


/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision: 1.5 $
 */
public class JoinDescriptor extends AbstractDescriptor implements Validatable {
    //~ Instance fields ////////////////////////////////////////////////////////

    protected List conditions = new ArrayList();
    protected ResultDescriptor result;

    //~ Constructors ///////////////////////////////////////////////////////////

    public JoinDescriptor() {
    }


    //~ Methods ////////////////////////////////////////////////////////////////

    public List getConditions() {
        return conditions;
    }

    public void setResult(ResultDescriptor result) {
        this.result = result;
    }

    public ResultDescriptor getResult() {
        return result;
    }

    public void validate() throws InvalidWorkflowDescriptorException {
        ValidationHelper.validate(conditions);

        if (result == null) {
            throw new InvalidWorkflowDescriptorException("Join has no result");
        }

        result.validate();
    }

    public String getXML() {
    	StringBuffer buf = new StringBuffer();
        buf.append("<join id=\"" + getId() + "\">");

        if (conditions.size() > 0) {
            for (int i = 0; i < conditions.size(); i++) {
                ConditionsDescriptor condition = (ConditionsDescriptor) conditions.get(i);
                buf.append(condition.getXML());
            }
        }

        buf.append(result.getXML());
        buf.append("</join>");
        return buf.toString();
    }

}
